package com.dcbeida.widget;


import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.dcbeida.R;
import com.dcbeida.bean.Area;
import com.dcbeida.utils.Constants;
import com.dcbeida.widget.wheel.AbstractWheelTextAdapter;
import com.dcbeida.widget.wheel.OnWheelChangedListener;
import com.dcbeida.widget.wheel.OnWheelClickedListener;
import com.dcbeida.widget.wheel.WheelView;

import java.util.List;
import java.util.Map;


public class AreaWheelDialog extends Dialog implements OnWheelChangedListener,OnWheelClickedListener {

	private Context mContext;
	private AreaSelectListener listener;
	private Area currentProvince;
	private Area currentCity;
	private Area currentArea;
	private Map<String,List<Area>> allCities;
	private List<Area> provinces;
	private Map<String,List<Area>> allAreas;

	private WheelView wheelView1;
	private WheelView wheelView2;
	private WheelView wheelView3;

	public AreaWheelDialog(Context context) {
		super(context, R.style.myDialogTheme);
		// TODO Auto-generated constructor stub
		mContext = context;
//		this.listener = listener;
		this.provinces = Constants.provinces;
		this.allCities = Constants.allCities;
		this.allAreas = Constants.areas;
//		initData();
		initView();
	}

	public void showDialog(Area province,Area city,Area area){
		if (province != null && city != null && area != null) {
			List<Area> cityList = null;
			List<Area> areaList = null;
			for (int i = 0; i < provinces.size(); i++) {
				if (province.getCode().equals(provinces.get(i).getCode())) {
					wheelView1.setCurrentItem(i);
					cityList = allCities.get(province.getCode());
					break;
				}
			}

			if (cityList != null) {
				for (int i = 0; i < cityList.size(); i++) {
					if (city.getCode().equals(cityList.get(i).getCode())) {
						wheelView2.setCurrentItem(i);
						areaList = allAreas.get(city.getCode());
						break;
					}
				}
			}

			if (areaList != null) {
				for (int i = 0; i < areaList.size(); i++) {
					if (area.getCode().equals(areaList.get(i).getCode())) {
						wheelView3.setCurrentItem(i);
						break;
					}
				}
			}





			this.currentProvince = province;
			this.currentCity = city;
			this.currentArea = area;
		}

		show();
	}

	public void showDialog(String province,String city,String area){
		if (!TextUtils.isEmpty(province) && !TextUtils.isEmpty(city) && !TextUtils.isEmpty(area)) {
			List<Area> cityList = null;
			List<Area> areaList = null;
			for (int i = 0; i < provinces.size(); i++) {
				if (province.equalsIgnoreCase(provinces.get(i).getName())) {
					wheelView1.setCurrentItem(i);
					this.currentProvince = provinces.get(i);
					cityList = allCities.get(provinces.get(i).getCode());
					break;
				}
			}

			if (cityList != null) {
				for (int i = 0; i < cityList.size(); i++) {
					if (city.equalsIgnoreCase(cityList.get(i).getName())) {
						wheelView2.setCurrentItem(i);
						this.currentCity = cityList.get(i);
						areaList = allAreas.get(cityList.get(i).getCode());
						break;
					}
				}
			}

			if (areaList != null) {
				for (int i = 0; i < areaList.size(); i++) {
					if (area.equalsIgnoreCase(areaList.get(i).getName())) {
						this.currentArea = areaList.get(i);
						wheelView3.setCurrentItem(i);
						break;
					}
				}
			}

		}

		show();
	}

	public void setListener(AreaSelectListener listener){
		this.listener = listener;
	}

	private void initView(){
		LayoutInflater inflater = getLayoutInflater();
		View layout = inflater.inflate(R.layout.dialog_area_selecter, null);

		layout.findViewById(R.id.tvCancl).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						cancel();
					}
				});

		layout.findViewById(R.id.tvConfirm).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						// 确认
						if (listener != null) {
							if (currentCity == null){
								currentCity = new Area();
							}

							if (currentArea == null){
								currentArea = new Area();
							}

							listener.area_selected(currentProvince, currentCity, currentArea);
						}
						cancel();
					}
				});

		setContentView(layout);

		wheelView1 = (WheelView) layout.findViewById(R.id.wheelview1);
		wheelView1.setVisibleItems(5);
		wheelView1.setWheelBackground(R.drawable.wheel_bg_holo);
		wheelView1.setWheelForeground(R.drawable.wheel_val_holo2);
		// wheelView1.setShadowColor(0xFF000000, 0x88000000, 0x00000000);
		wheelView1.setViewAdapter(new MyWheelAdapter(mContext, provinces));
		wheelView1.addClickingListener(this);
		wheelView1.addChangingListener(this);

		wheelView2 = (WheelView) layout.findViewById(R.id.wheelview2);
		wheelView2.setVisibleItems(5);
		wheelView2.setWheelBackground(R.drawable.wheel_bg_holo);
		wheelView2.setWheelForeground(R.drawable.wheel_val_holo2);
		// wheelView2.setShadowColor(0xFF000000, 0x88000000, 0x00000000);

		currentProvince = provinces.get(0);

		if (currentProvince != null){
			List<Area> cityList = allCities.get(currentProvince.getCode());
			wheelView2.setViewAdapter(new MyWheelAdapter(mContext, cityList));
			wheelView2.addClickingListener(this);
			wheelView2.addChangingListener(this);
			if (cityList != null) {
				currentCity = cityList.get(0);
			}
		}





		wheelView3 = (WheelView) layout.findViewById(R.id.wheelview3);
		wheelView3.setVisibleItems(5);
		wheelView3.setWheelBackground(R.drawable.wheel_bg_holo);
		wheelView3.setWheelForeground(R.drawable.wheel_val_holo2);
		// wheelView2.setShadowColor(0xFF000000, 0x88000000, 0x00000000);

		if (currentCity != null){
			List<Area> areaList = allAreas.get(currentCity.getCode());
			wheelView3.setViewAdapter(new MyWheelAdapter(mContext, areaList));
			wheelView3.addClickingListener(this);
			wheelView3.addChangingListener(this);
			if (areaList != null) {
				currentArea = areaList.get(0);
			}
		}




		// 设置对话框的出现位置，借助于window对象
		Window win = getWindow();
		win.setGravity(Gravity.BOTTOM);
		// win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//
		// 弹出对话框时，底部窗体，不变暗。

		WindowManager.LayoutParams lp = win.getAttributes();
		// lp.x = -200;// x=0,y=0时，显示位置是屏幕中心。
		// lp.y = 0;
		// lp.alpha = 0.6f;// 对话框的透明度
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		win.setAttributes(lp);
		win.setWindowAnimations(R.style.dialoganimstyle);
		setCancelable(false);
	}

	private class MyWheelAdapter extends AbstractWheelTextAdapter {

		private List<Area> data;

		protected MyWheelAdapter(Context context, List<Area> data) {
			super(context, R.layout.wheel_item, NO_RESOURCE);
			// TODO Auto-generated constructor stub
			setItemTextResource(R.id.tvItem);
			this.data = data;
		}

		@Override
		public int getItemsCount() {
			// TODO Auto-generated method stub
			if (data !=null)
				return data.size();
			else
				return 0;
		}

		@Override
		protected CharSequence getItemText(int index) {
			if (data !=null)
				return data.get(index).getName();
			else
				return "";
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
			return view;
		}
	}




	@Override
	public void onItemClicked(WheelView wheel, int itemIndex) {
		// TODO Auto-generated method stub
		wheel.setCurrentItem(itemIndex);
	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		// TODO Auto-generated method stub
		int pos = wheel.getCurrentItem();
		if (wheelView1 == wheel) {
			//省份
			currentProvince = provinces.get(pos);

			List<Area> cityList = allCities.get(currentProvince.getCode());
			wheelView2.setViewAdapter(new MyWheelAdapter(mContext, cityList));
			if (cityList != null){
				currentCity = cityList.get(0);
				wheelView2.setCurrentItem(0);
			}else {
				currentCity = new Area();
			}

			if (currentCity != null){
				List<Area> areaList = allAreas.get(currentCity.getCode());
				wheelView3.setViewAdapter(new MyWheelAdapter(mContext, areaList));
				if (areaList != null){
					currentArea = areaList.get(0);
					wheelView3.setCurrentItem(0);
				}else {
					currentArea = new Area();
				}
			}else {
				currentArea = new Area();
				wheelView3.setViewAdapter(new MyWheelAdapter(mContext, null));
			}






		}else if(wheelView2 == wheel){
			//城市
			currentCity = allCities.get(currentProvince.getCode()).get(pos);

			if (currentCity != null){
				List<Area> areaList = allAreas.get(currentCity.getCode());
				wheelView3.setViewAdapter(new MyWheelAdapter(mContext, areaList));
				if (areaList != null){
					currentArea = areaList.get(0);
					wheelView3.setCurrentItem(0);
				}else
					currentArea = new Area();
			}else {
				wheelView3.setViewAdapter(new MyWheelAdapter(mContext, null));
				currentArea = new Area();
			}

		}else {
			if (currentCity != null)
				currentArea = allAreas.get(currentCity.getCode()).get(pos);
			else
				currentArea = new Area();
		}
	}

	public interface AreaSelectListener {
		void area_selected(Area province, Area city, Area area);
	}
}
